package com.xt.website.dao;

import com.xt.website.po.CategorySecondExt;
import com.xt.website.po.Product;
import com.xt.website.po.ProductExt;
import com.xt.website.po.ProductFile;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: jiangyu
 * @Date: 2019/8/18 14:07
 * @Description:
 */
@Mapper
public interface ProductDao {
    /**
     * 根据一级类目，查询的产品列表
     */
    @Select("select p.* " +
            "from tb_category c inner join tb_categorysecond cs " +
            "on c.id = cs.cid " +
            "inner join tb_product p " +
            "on cs.id = p.csid where c.id = #{cid} order by p.create_time desc")
    @Results(id="productMap",value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "pro_name", property = "proName"),
            @Result(column = "pro_picture", property = "proPicture"),
            @Result(column = "pro_info", property = "proInfo"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "pro_href", property = "proHref"),
            @Result(column = "pro_kind", property = "proKind"),
            @Result(column = "csid", property = "csid")
    })
    List<Product> findProductsByCid(Integer cid);

    /**
     * 根据二级类目，查询的产品列表
     */
    @Select("select * from tb_product where csid=#{csid} order by create_time desc")
    @ResultMap("productMap")
    List<Product> findProductsByCsid(Integer csid);

    /**
     * 查询产品详情
     */
    @Select("select * from tb_product where id = #{pid}")
    @Results(id="productMapExt",value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "pro_name", property = "proName"),
            @Result(column = "pro_picture", property = "proPicture"),
            @Result(column = "pro_info", property = "proInfo"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "pro_href", property = "proHref"),
            @Result(column = "pro_kind", property = "proKind"),
            @Result(column = "csid", property = "csid"),
            @Result(column = "id", property = "list",
                    many = @Many(select = "com.xt.website.dao.ProductDao.getProductFileByPid"))
    })
    ProductExt<ProductFile> productDetails(Integer pid);


    /**
     * 查询产品下的全部附件
     */
    @Select("select * from tb_product_file where pid=#{pid}")
    ArrayList<ProductFile> getProductFileByPid(Integer pid);


}
